package com.ecnu.sei.app.course;

import com.ecnu.sei.app.Scheduler;
import com.ecnu.sei.app.resources.Classroom;
import com.ecnu.sei.app.resources.Time;
import com.ecnu.sei.app.util.Pair;
import com.ecnu.sei.app.util.Utils.CollegeYear;

public class CompulsoryCourse extends AbstractCourse {

  private final int COMPULSORY_PROCTOR_NUM = 3;

  public CompulsoryCourse(CourseInfo info) {
    super(info);
  }

  @Override
  public boolean setResource() { 
    Pair<Time, Classroom> resource = scheduler.getFreeResource(info.getCollegeYear());

    if (resource != null) {
      String className = info.getClassName();
      Time time = resource.getFirst(); 
      if (!scheduler.hasExamAtTime(className, time)) {
        setResource(resource);
        return true;
      }
    }
    return false;
  }

  private void setResource(Pair<Time, Classroom> resource) {
    ExamInfo examInfo = info.getExamInfo();
    examInfo.setTime(resource.getFirst()).setClassroom(resource.getSecond());
  }

  @Override
  public boolean setResourcePending() {
    for (CollegeYear collegeYear : CollegeYear.getList()) {
      Pair<Time, Classroom> resource = scheduler.getFreeResource(collegeYear);
      if (resource != null) {
        String className = info.getClassName();
        Time time = resource.getFirst();
        if (!scheduler.hasExamAtTime(className, time)) {
          setResource(resource);
          return true;
        }
      }
    }
    return false;
  }

  @Override
  public int getProctorNum() {
    return COMPULSORY_PROCTOR_NUM;
  }

}
